package com.hoshiicloud.market.service.impl.activities;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.market.entity.activities.Slideshow;
import com.hoshiicloud.market.feign.IdGeneratorService;
import com.hoshiicloud.market.mapper.activities.SlideshowMapper;
import com.hoshiicloud.market.service.activities.SlideshowService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author:campchen
 * @Date:Created in 下午 04:53 2019/8/26
 * @PACKAGENAME com.hoshiicloud.market.service.impl.activities
 * @Description
 */
@Service
public class SlideshowServiceImpl extends ServiceImpl<SlideshowMapper, Slideshow> implements SlideshowService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    @Override
    @Cacheable(cacheNames = "Slideshow", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "根据Id查询轮播图信息 ")
    public Slideshow getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public String insert(Slideshow entity) {
        return null;
    }

    @Override
    @CacheEvict(cacheNames = "Slideshow", allEntries = true)
    @Logable(authName = "新增店铺轮播图信息")
    public boolean save(Slideshow entity) {
        // 生成主键ID
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("新增失败，生成主键Id异常"));
        }

        // 新增数据
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());

        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    @Override
    @CacheEvict(cacheNames = "Slideshow", allEntries = true)
    @Logable(authName = "根据id店铺轮播图信息")
    public boolean updateById(Slideshow entity){
        entity.setUpdatedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "Slideshow", allEntries = true)
    @Logable(authName = "根据id店铺轮播图信息")
    public boolean deleteById(Long id) {

        // 根据id查询数据
        Slideshow entity = this.getById(id);
        if (entity == null) {
            throw new ServiceException("删除根据id店铺轮播图信息失败，无法找到相关记录");
        }

        //进行删除操作
        entity.setUpdatedTime(LocalDateTime.now());
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }



    @Override
    @Logable(authName = "店铺轮播图")
    @Cacheable(cacheNames = "Slideshow", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    public Page<Slideshow> getPage(Page<Slideshow> page, QueryWrapper<Slideshow> queryWrapper, String key) {
        baseMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Logable(authName = "营销服务-banner图片管理-查询banner图片")
    @Cacheable(cacheNames = "Slideshow", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Override
    public List<Slideshow> list(Wrapper<Slideshow> queryWrapper, String key) {
        return baseMapper.selectList(queryWrapper);
    }
}
